<?php
/**
 * Class SystemAction
 * 系统管理类
 */
Class SystemAction extends Action {
    public function index(){
        $user = session('user');
        $this->assign('xiamiUrl',C('XIAMI_URL'));
        if($user == null){
            $this->redirect('login');
        }else{
            $this->display();//输出页面模板
        }
    }

    /**
     * 验证码
     */
    public function code(){
        import('ORG.Util.Image');
        Image::buildImageVerify();
    }
    /**
     * 登录
     */
    public function login(){
        $this->display();
    }
    /**
     * 退出
     */
    public function logout(){
        session(null);
        $this->redirect('login');
    }
    /**
     * 登录表单提交 Ajax
     */
    public function toLogin(){
        $name = I('username');
        $pwd = I('password');
        $code = I('code');

        $SessionCode = session('verify');

        if(md5($code) == $SessionCode){
            $u = M('user');
            $user = $u->where('username="'.$name.'"')->find();
            if($user == null){
                $this->show('用户不存在');
            }else{
                if($user['password'] != md5($pwd)){
                   $this->show('密码输入错误，请重新输入');
                }else{
                    session(array('expire'=>3600));
                    session('user',$user);
                    $this->show('null');
                }
            }
        }else{
            $this->show('验证码输入错误');
        }
    }
}